/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views.Dialog;

import controllers.entity.ChamThi;
import controllers.entity.GiangDay;
import controllers.process.CanBoProcess;
import controllers.process.ChamThiProcess;
import controllers.process.GiangDayProcess;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import models.Constant;
import views.JPanel.TTCanBo;

/**
 *
 * @author HoaDuyen
 */
public class DlgThemGiangDay extends java.awt.Dialog {

    private CanBoProcess canBoProcess;
    private GiangDayProcess giangDayProcess;
    private int macanbo;
    private GiangDay giangDay;
    private TTCanBo parent;
    public DlgThemGiangDay(JFrame parent, boolean modal,int macanbo,GiangDay giangDay ) {
        super(parent, modal);
        this.parent=(TTCanBo)parent;
        this.giangDay=giangDay;
        this.macanbo=macanbo;
        this.setResizable(false);
        canBoProcess=new CanBoProcess();
        this.setLocation(Constant.LOCATE_XDLG, Constant.LOCATE_YDLG);
        giangDayProcess=new GiangDayProcess();
        initComponents();
        ok.setText("Thêm");
        if (giangDay != null) { // Dang o che do sua
            malop.setText(""+giangDay.getMaLopHoc());
            tenmonhoc.setText(giangDay.getTenMonHoc());
            sotinchi.setText(""+giangDay.getSoTinChi());
            sosinhvien.setText(""+giangDay.getSoSinhVien());
            hocky.setText(""+giangDay.getHocKy());
            namhoc.setText(""+giangDay.getYear());
            txtGiangDay.setText("Sửa Thông Tin Giảng Dạy");
            mamonhoc.setText(giangDay.getMaMonHoc());
            mamonhoc.setEnabled(false);
            ok.setText("Lưu");
        }
    }
    public boolean kiemtrasonguyen(String s){
        int n=s.length();
        for (int i=0;i<n;i++)
            if (s.charAt(i)<'0'||s.charAt(i)>'9') return false;
        return true;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel16 = new javax.swing.JPanel();
        jLabel26 = new javax.swing.JLabel();
        jLabel27 = new javax.swing.JLabel();
        jLabel28 = new javax.swing.JLabel();
        malop = new javax.swing.JTextField();
        tenmonhoc = new javax.swing.JTextField();
        sotinchi = new javax.swing.JTextField();
        jLabel29 = new javax.swing.JLabel();
        mamonhoc = new javax.swing.JTextField();
        jPanel17 = new javax.swing.JPanel();
        jLabel30 = new javax.swing.JLabel();
        jLabel31 = new javax.swing.JLabel();
        jLabel32 = new javax.swing.JLabel();
        namhoc = new javax.swing.JTextField();
        hocky = new javax.swing.JTextField();
        sosinhvien = new javax.swing.JTextField();
        txtGiangDay = new javax.swing.JLabel();
        ok = new javax.swing.JButton();
        cancel = new javax.swing.JButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jLabel26.setText("Mã lớp");

        jLabel27.setText("Tên môn học");

        jLabel28.setText("Số tín chỉ");

        sotinchi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sotinchiActionPerformed(evt);
            }
        });

        jLabel29.setText("Mã môn học");

        javax.swing.GroupLayout jPanel16Layout = new javax.swing.GroupLayout(jPanel16);
        jPanel16.setLayout(jPanel16Layout);
        jPanel16Layout.setHorizontalGroup(
            jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel16Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel16Layout.createSequentialGroup()
                        .addComponent(jLabel29)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(mamonhoc, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel16Layout.createSequentialGroup()
                        .addGroup(jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel27)
                            .addComponent(jLabel26)
                            .addComponent(jLabel28))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(sotinchi, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(malop, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tenmonhoc, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        jPanel16Layout.setVerticalGroup(
            jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel16Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel26)
                    .addComponent(malop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel27)
                    .addComponent(tenmonhoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel28)
                    .addComponent(sotinchi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel29)
                    .addComponent(mamonhoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel30.setText("Số sinh viên");

        jLabel31.setText("Học kỳ");

        jLabel32.setText("Năm Học");

        sosinhvien.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sosinhvienActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel17Layout = new javax.swing.GroupLayout(jPanel17);
        jPanel17.setLayout(jPanel17Layout);
        jPanel17Layout.setHorizontalGroup(
            jPanel17Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel17Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel17Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel30)
                    .addComponent(jLabel32)
                    .addComponent(jLabel31))
                .addGap(33, 33, 33)
                .addGroup(jPanel17Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(hocky, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sosinhvien, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(namhoc, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        jPanel17Layout.setVerticalGroup(
            jPanel17Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel17Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(jPanel17Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel30)
                    .addComponent(sosinhvien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel17Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel31)
                    .addComponent(hocky, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel17Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel32)
                    .addComponent(namhoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        txtGiangDay.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        txtGiangDay.setText("Thêm Giảng Dạy");

        ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btn_add.png"))); // NOI18N
        ok.setText("Thêm");
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        cancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btn_delete.png"))); // NOI18N
        cancel.setText("Hủy");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(179, 179, 179)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(ok)
                        .addGap(18, 18, 18)
                        .addComponent(cancel))
                    .addComponent(txtGiangDay))
                .addContainerGap(166, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel16, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(49, 49, 49)
                    .addComponent(jPanel17, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(24, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(txtGiangDay)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 199, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ok)
                    .addComponent(cancel))
                .addGap(29, 29, 29))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(80, 80, 80)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel17, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jPanel16, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(64, Short.MAX_VALUE)))
        );

        add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Closes the dialog
     */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void sosinhvienActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sosinhvienActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_sosinhvienActionPerformed

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        if (malop.getText().length()>20){
            JOptionPane.showMessageDialog(this,"Kích thước không hợp lệ !");
            malop.selectAll();
            malop.requestFocus();
            return;
        } 
        if (tenmonhoc.getText().length()>40){
            JOptionPane.showMessageDialog(this,"Kích thước không hợp lệ !");
            tenmonhoc.selectAll();
            tenmonhoc.requestFocus();
            return;
        } 
        if (malop.getText().equals("") ||
            tenmonhoc.getText().equals("")||
            sotinchi.getText().equals("")||
            sosinhvien.getText().equals("")||
            hocky.getText().equals("")||
             namhoc.getText().equals("")||
              mamonhoc.getText().equals("")){
           JOptionPane.showMessageDialog(this, "Không được bỏ trống!"); 
           return;
        }      
        String tmalop =malop.getText();
        String ttenmonhoc=tenmonhoc.getText();
        Integer tsotinchi,tsosinhvien,tnamhoc,thocky;
        try {
                    tsotinchi = Integer.parseInt(sotinchi.getText());
        } catch (Exception e1) {
                    JOptionPane.showMessageDialog(null, "Phải nhập số nguyên !");
                    sotinchi.selectAll();
                    sotinchi.requestFocus();
                    return;
        }
        if (tsotinchi<0){
            JOptionPane.showMessageDialog(null, "Số tín chỉ không được <0!");
                    sotinchi.selectAll();
                    sotinchi.requestFocus();
                    return;
        }
        try {
                    tsosinhvien= Integer.parseInt(sosinhvien.getText());
        } catch (Exception e1) {
                    JOptionPane.showMessageDialog(null, "Phải nhập số nguyên !");
                    sosinhvien.selectAll();
                    sosinhvien.requestFocus();
                    return;
        }
        if (tsosinhvien<0 ||tsosinhvien>400){
            JOptionPane.showMessageDialog(null, "Số sinh viên không được <0 hoặc >400!");
                    sosinhvien.selectAll();
                    sosinhvien.requestFocus();
                    return;
        }
        try {
                    thocky= Integer.parseInt(hocky.getText());
        } catch (Exception e1) {
                    JOptionPane.showMessageDialog(null, "Phải nhập số nguyên !");
                    hocky.selectAll();
                    hocky.requestFocus();
                    return;
        }
        if (thocky<0||thocky>15){
             JOptionPane.showMessageDialog(null, "Học kỳ không chính xác!");
                    hocky.selectAll();
                    hocky.requestFocus();
                    return;
        }
        try {
                    tnamhoc= Integer.parseInt(namhoc.getText());
        } catch (Exception e1) {
                    JOptionPane.showMessageDialog(null, "Phải nhập số nguyên !");
                    namhoc.selectAll();
                    namhoc.requestFocus();
                    return;
        }
        if (tnamhoc<1900||tnamhoc>3000){
            JOptionPane.showMessageDialog(null, "Năm học sai");
            namhoc.selectAll();
            namhoc.requestFocus();
            return;
        }
        
        String tmamonhoc=mamonhoc.getText();
        
            GiangDay ct=new GiangDay(macanbo,tmamonhoc,ttenmonhoc,tsotinchi,tmalop,tsosinhvien,thocky,tnamhoc);
            if (giangDay == null) { // Them moi sach
                if (GiangDayProcess.insertGiangDay(ct)) {
                    JOptionPane.showMessageDialog(this, "Thêm thành công!");
                    parent.loadListGiangDay();
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(this, "Lỗi,kiểm tra lại SQL service!");
                }
            } else {
                //sua sach
                if (GiangDayProcess.updateGiangDay(ct)) {
                    JOptionPane.showMessageDialog(this, "Sửa thành công!");
                    parent.loadListGiangDay();
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(this, "Lỗi,kiểm tra lại SQL service!");
                }
            }
        
    }//GEN-LAST:event_okActionPerformed

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        dispose();
    }//GEN-LAST:event_cancelActionPerformed

    private void sotinchiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sotinchiActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_sotinchiActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancel;
    private javax.swing.JTextField hocky;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel29;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel31;
    private javax.swing.JLabel jLabel32;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel16;
    private javax.swing.JPanel jPanel17;
    private javax.swing.JTextField malop;
    private javax.swing.JTextField mamonhoc;
    private javax.swing.JTextField namhoc;
    private javax.swing.JButton ok;
    private javax.swing.JTextField sosinhvien;
    private javax.swing.JTextField sotinchi;
    private javax.swing.JTextField tenmonhoc;
    private javax.swing.JLabel txtGiangDay;
    // End of variables declaration//GEN-END:variables
}
